﻿Imports System.IO

Public Class tyranntGeneratorForm

    Private _dialogPath As String
    Private _currentMaze As Integer = 0

    Private Sub SettingsToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles SettingsToolStripMenuItem.Click
        Dim settingsDlg As New settingsDialog
        If settingsDlg.ShowDialog() = Windows.Forms.DialogResult.OK Then
            _dialogPath = settingsDlg.SDFilePath
            ShowPath()
        End If
    End Sub

    Private Sub tyranntGeneratorForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        ShowPath()
    End Sub

    Private Sub ShowPath()
        pathToolStripTextBox.Text = "Path: " + _dialogPath
    End Sub

    Private Sub itemPictureBox_Click(sender As System.Object, e As System.EventArgs) Handles itemPictureBox.Click
        Dim fileRequester As New OpenFileDialog()
        fileRequester.InitialDirectory = _dialogPath + "\images\inventory\image"
        fileRequester.Multiselect = False
        fileRequester.CheckFileExists = True
        fileRequester.DefaultExt = ".gif"
        fileRequester.Title = "Select a .Gif file"
        If fileRequester.ShowDialog = Windows.Forms.DialogResult.OK Then
            Dim img As New Bitmap(fileRequester.FileName)
            itemPictureBox.Image = img
            itemPictureBox.Tag = Path.GetFileName(fileRequester.FileName)
        End If
    End Sub

    Private Sub itemIconPictureBox_Click(sender As System.Object, e As System.EventArgs) Handles itemIconPictureBox.Click
        Dim fileRequester As New OpenFileDialog()
        fileRequester.InitialDirectory = _dialogPath + "\images\inventory\icon"
        fileRequester.Multiselect = False
        fileRequester.CheckFileExists = True
        fileRequester.DefaultExt = ".gif"
        fileRequester.Title = "Select a .Gif file"
        If fileRequester.ShowDialog = Windows.Forms.DialogResult.OK Then
            Dim img As New Bitmap(fileRequester.FileName)
            itemIconPictureBox.Image = img
            itemIconPictureBox.Tag = Path.GetFileName(fileRequester.FileName)
        End If
    End Sub

    Private Sub itemNewButton_Click(sender As System.Object, e As System.EventArgs) Handles itemNewButton.Click
        NewItem()
    End Sub

    Private Sub NewItem()
        itemIconPictureBox.Tag = ""
        itemIconPictureBox.Image = Nothing
        itemPictureBox.Tag = ""
        itemPictureBox.Image = Nothing
        itemNameTextBox.Text = ""
        requiresTextBox.Text = ""
        itemTypeComboBox.Text = "general"
        itemCountNumericUpDown.Value = 1
        itemUsableCheckBox.Checked = False
        isQuestItemCheckBox.Checked = False
        itemBrokenCheckBox.Checked = False
        itemValueTextBox.Text = ""
        itemDescriptionTextBox.Text = ""
    End Sub

    Private Sub itemLoadButton_Click(sender As System.Object, e As System.EventArgs) Handles itemLoadButton.Click
        Dim rawItem As String = GetItem()
        If IsNothing(rawItem) = False Then
            LoadItem(rawItem)
        End If

    End Sub

    Private Function GetItem() As String
        Dim fileRequester As New OpenFileDialog
        fileRequester.InitialDirectory = _dialogPath + "\items\store"
        fileRequester.Multiselect = False
        fileRequester.CheckFileExists = True
        fileRequester.DefaultExt = ".itm"
        fileRequester.Title = "Select an Item file"
        If fileRequester.ShowDialog = Windows.Forms.DialogResult.OK Then
            Dim objReader As New System.IO.StreamReader(fileRequester.FileName)
            Dim result As String = objReader.ReadToEnd
            objReader.Close()
            Return result
        Else
            Return Nothing
        End If

    End Function

    Private Sub LoadItem(rawItem As String)
        Dim bits() As String = rawItem.Split("|"c)
        If bits.Length <> 12 Then
            MessageBox.Show("Error Item is invalid")
            Exit Sub
        End If
        If bits(0) <> "1.0" Then
            MessageBox.Show("Error Item is not version 1.0")
            Exit Sub
        End If
        itemNameTextBox.Text = bits(1)
        requiresTextBox.Text = bits(2)
        itemDescriptionTextBox.Text = bits(3)
        LoadIconImage(bits(4))
        LoadItemImage(bits(5))
        itemTypeComboBox.Text = bits(6)
        Try
            itemCountNumericUpDown.Value = Integer.Parse(bits(7))
        Catch ex As Exception
            MessageBox.Show("Error item count invalid")
            itemCountNumericUpDown.Value = 1
        End Try
        itemValueTextBox.Text = bits(8)
        Try
            itemUsableCheckBox.Checked = Boolean.Parse(bits(9))
        Catch ex As Exception
            MessageBox.Show("Error item usable bool invalid")
            itemUsableCheckBox.Checked = False
        End Try
        Try
            isQuestItemCheckBox.Checked = Boolean.Parse(bits(10))
        Catch ex As Exception
            MessageBox.Show("Error is quest item bool invalid")
            isQuestItemCheckBox.Checked = False
        End Try
        Try
            itemBrokenCheckBox.Checked = Boolean.Parse(bits(11))
        Catch ex As Exception
            MessageBox.Show("Error item broken bool invalid")
            itemBrokenCheckBox.Checked = False
        End Try
    End Sub

    Private Sub LoadItemImage(imageName As String)
        Dim fullPath As String = _dialogPath + "\images\inventory\image\" + imageName
        Dim img As New Bitmap(fullPath)
        itemPictureBox.Image = img
        itemPictureBox.Tag = imageName
    End Sub

    Private Sub LoadIconImage(imageName As String)
        Dim fullPath As String = _dialogPath + "\images\inventory\icon\" + imageName
        Dim img As New Bitmap(fullPath)
        itemIconPictureBox.Image = img
        itemIconPictureBox.Tag = imageName
    End Sub

    Private Sub findItemButton_Click(sender As System.Object, e As System.EventArgs) Handles findItemButton.Click
        Dim rawItem As String = GetItem()
        If IsNothing(rawItem) = False Then
            Dim bits() As String = rawItem.Split("|"c)
            requiresTextBox.Text = bits(1)
        End If
    End Sub

    Private Sub itemSaveButton_Click(sender As System.Object, e As System.EventArgs) Handles itemSaveButton.Click
        Dim rawItem As String = "1.0"
        rawItem += "|" + itemNameTextBox.Text
        rawItem += "|" + requiresTextBox.Text
        rawItem += "|" + itemDescriptionTextBox.Text
        rawItem += "|" + CType(itemIconPictureBox.Tag.ToString, String)
        rawItem += "|" + CType(itemPictureBox.Tag, String)
        rawItem += "|" + itemTypeComboBox.Text
        rawItem += "|" + itemCountNumericUpDown.Value.ToString
        rawItem += "|" + itemValueTextBox.Text
        rawItem += "|" + itemUsableCheckBox.Checked.ToString
        rawItem += "|" + isQuestItemCheckBox.Checked.ToString
        rawItem += "|" + itemBrokenCheckBox.Checked.ToString

        Dim fname As String = _dialogPath + "\items\store\" + itemNameTextBox.Text + ".itm"

        Dim objWriter As New System.IO.StreamWriter(fname)
        objWriter.Write(rawItem)
        objWriter.Close()

    End Sub

    Private Sub loadMazeButton_Click(sender As System.Object, e As System.EventArgs) Handles loadMazeButton.Click
        Dim fname As String = _dialogPath + "\maps\store\" + mazeLevelNumericUpDown.Value.ToString() + ".map"
        Dim objreader As System.IO.StreamReader
        Try
            objreader = New System.IO.StreamReader(fname)
        Catch ex As Exception
            MessageBox.Show("Maze does not exist")
            Exit Sub
        End Try

        ' get the header section
        Dim line As String
        line = objReader.ReadLine
        Dim header() As String = line.Split("|"c)
        Dim version As String = header(0)
        If header.Length <> 3 Then
            MessageBox.Show("Invalid Maze Header")
            Exit Sub
        End If
        If version <> "1.0" Then
            MessageBox.Show("Invalid maze version")
            Exit Sub
        End If
        Try
            mazeWidthNumericUpDown.Value = Integer.Parse(header(1))
            mazeHeightNumericUpDown.Value = Integer.Parse(header(2))
        Catch ex As Exception
            MessageBox.Show("Invalid maze file")
        End Try

        ' Now load the the maze text
        mazeTextBox.Text = objReader.ReadToEnd
        objReader.Close()
    End Sub

    Private Sub mazeLevelNumericUpDown_ValueChanged(sender As System.Object, e As System.EventArgs) Handles mazeLevelNumericUpDown.ValueChanged
        _currentMaze = CType(mazeLevelNumericUpDown.Value, Integer)
    End Sub

    Private Sub saveMazeButton_Click(sender As System.Object, e As System.EventArgs) Handles saveMazeButton.Click
        ' Generate maze file
        Dim width As Integer = CType(mazeWidthNumericUpDown.Value, Integer)
        Dim height As Integer = CType(mazeHeightNumericUpDown.Value, Integer)
        Dim maze(height) As String
        Dim line As String
        ' generate the header part which shows the width and height of the maze.
        line = "1.0|" + mazeWidthNumericUpDown.Value.ToString + "|" + mazeHeightNumericUpDown.Value.ToString
        maze(0) = line
        ' Now generate the actual maze 
        Dim pos As Integer = 1
        For Each txtLine As String In mazeTextBox.Lines
            If txtLine.Length < (width) Then
                MessageBox.Show("Line " + pos.ToString() + " is too short")
                Exit Sub
            End If
            maze(pos) = txtLine.Substring(0, width)
            If pos > height Then
                Exit For
            End If
            pos += 1
        Next
        Dim fname As String = _dialogPath + "\maps\store\" + mazeLevelNumericUpDown.Value.ToString + ".map"

        Dim objWriter As New System.IO.StreamWriter(fname)

        ' write the maze data to disk
        For Each txtLine As String In maze
            objWriter.WriteLine(txtLine)
        Next
        objWriter.Close()

    End Sub

End Class
